<%-- 
    Document   : administracaoGruposCadastrar
    Created on : 11/11/2010, 06:17:01
    Author     : Thiago
--%>

<%@page import="dao.*"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="crypt.Crypt"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="bo.*"%>
<%@page import="beans.*"%>
<%@page import="autenticacao.Sessao"%>
<%
            String moduleId = "27";
            Crypt crypt = new Crypt();
            try {
                crypt.setKey(crypt.Decifragem((String) session.getAttribute("userPhrase")));
                String desafio = crypt.Cifragem(moduleId + "@" + ferramentas.DataHora.formatarDataHora(ferramentas.DataHora.retornarDataHoraAtual(), ferramentas.DataHora.DATE_TIME_FORMAT));
                if (!(new Sessao(session).validarSessao(desafio))) {
                    response.sendRedirect("/" + request.getContextPath());
                }
            } catch (Exception e) {
                response.sendRedirect("/" + request.getContextPath());
            }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Gestor Operacional</title>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/style.css" type="text/css" rel="stylesheet"/>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/table.css" type="text/css" rel="stylesheet"/>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/anytime.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/jquery.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/anytime.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/anytimetz.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/table.js"></script>
    </head>
    <body>
        <table width="100%">
            <tr>
                <td>
                    <jsp:include page = "../menu.jsp" flush = "true"/>
                </td>
            </tr>

            <tr align="center">
                <td><br/><br/>
                    <form name="form1" name="form1" method="post" action="administracaoGruposCadastrar.jsp">
                        <input type="hidden" name="action" value="executar"/>
                        <%
                                    String action = (String) request.getParameter("action");
                                    if ((action != null) && (action.equalsIgnoreCase("executar"))) {
                                        try {
                                            Colaborador colab = new Colaborador();
                                            colab.setMatricula((String) request.getParameter("matricula"));
                                            TipoGrupoInspecao tgi = new TipoGrupoInspecao();
                                            tgi.setColaborador(colab);
                                            ArrayList<TipoGrupoInspecao> atgi = new TipoGrupoInspecaoDAO().recuperarTipoGrupoInspecao(tgi);
                                            if ((atgi == null) || (atgi.isEmpty())) {
                                                tgi.setSite(new Site());
                                                tgi.getSite().setId(Integer.parseInt((String) request.getParameter("site")));
                                                tgi.setDescricao((String) request.getParameter("descricao"));
                                                new TipoGrupoInspecaoDAO().manterTipoGrupoInspecao(tgi);
                                                out.println("<script language=\"JavaScript\"> window.location.replace('" + (new String(request.getRequestURL()).split("pages")[0]) + "pages/administracaoGruposEditar.jsp?id=" + tgi.getId() + "'); </script>");
                                                out.flush();
                                            } else {
                                                out.println("<strong><center>Colaborador já responsável pela equipe: " + (((atgi == null) && (!atgi.isEmpty())) ? atgi.get(0).getDescricao() : "-") + ".</center></strong><br>");
                                            }
                                        } catch (Exception e) {
                                            System.out.println(e.getMessage());
                                            out.println("<strong><center>Falha na Inserção: " + e.getMessage() + ".</center></strong><br>");
                                        }
                                    }
                        %>
                        <table border="1" cellspacing="0" width="450" bgcolor="#FFFFFF">
                            <tr>
                                <td width="150">Nome do Grupo:</td>
                                <td align="center"><input type="text" size="30" name="descricao"></td>
                            </tr>
                            <tr>
                            <tr><td>Site:</td>
                                <td align="center">
                                    <select name="site" size="1">
                                        <option value="" selected>Todos</option>
                                        <%
                                                    ArrayList<Site> als = null;
                                                    try {
                                                        als = new SiteBO().recuperarSite(null);
                                                    } catch (Exception e) {
                                                        out.println(e.getMessage());
                                                    }
                                                    if (als != null) {
                                                        for (int i = 0; i < als.size(); i++) {
                                                            out.println("<option value=\"" + als.get(i).getId() + "\">" + als.get(i).getDescricao() + "</option>");
                                                        }
                                                    }

                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>Responsável:</td>
                                <td align="center">
                                    <select name="matricula" size="1">
                                        <option value="" selected>Todos</option>
                                        <%
                                                    ArrayList<Colaborador> alc = null;
                                                    try {
                                                        alc = new ColaboradorBO().recuperarColaborador(null);
                                                    } catch (Exception e) {
                                                        out.println(e.getMessage());
                                                    }
                                                    if (alc != null) {
                                                        for (int i = 0; i < alc.size(); i++) {
                                                            out.println("<option value=\"" + alc.get(i).getMatricula() + "\">" + alc.get(i).getMatricula() + " - " + alc.get(i).getNome() + "</option>");
                                                        }
                                                    }
                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" align="center">
                                    <input type="submit" value="Cadastrar">
                                </td>
                            </tr>
                        </table>
                    </form>
                </td>
            </tr>
        </table>
    </body>
</html>